package user_logs

import (
	"encoding/json"
	"gitee.com/theegg/go-brick/app/service/app"
	"time"
)

var UserLogTypes = []map[string]interface{}{
	0: {
		"value": "0",
		"label": "登录",
		"field": []string{"username", "password", "ip", "user_agent"},
	},
	1: {
		"value": "1",
		"label": "冻结",
		"field": []string{"user_id", "lock_reason", "lock_level", "lock_type", "admin_id", "ip"},
	},
	2: {
		"value": "2",
		"label": "理赔代金券",
		"field": []string{"user_id", "amount", "admin_id", "ip", "user_agent"},
	},
}

func SaveLog(userId int64, typeId int, LogData map[string]interface{}) error {

	logModel := new(UserLogs)
	logModel.UserId = userId
	logModel.LogType = typeId
	jsonData, _ := json.Marshal(LogData)
	logModel.Content = string(jsonData)
	logModel.AddTime = time.Now().Unix()

	affected, errInsert := app.Db().Insert(logModel)
	if errInsert != nil {
		return errInsert
	}

	if affected != 1 {
		return app.NewError("sorry, wrong insert count!")
	}

	return nil
}
